'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFBxdCtrl
 * @description
 * # FinanceFBxdCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFBxdCtrl', function($scope, $rootScope, DataService, openApi, Util, $location, $routeParams, FormSubmit) {
        var selectData, params;
        $scope.param = {}
            // --------------------------------------
        $scope.sheetObj = {};
        // 底部关联模块类型选框添加
        var relModuleObj = {};
        relModuleObj.sheettitle = "关联模块";
        relModuleObj.callback = function(item) {
            if (item.selectTabContent == "无") {
                $scope.param.relatedModuleName = "无";
                $scope.param.relatedModule = "";
                $scope.param.moduleNo = "";
                return;
            }
            $scope.param.relatedModule = item.selectTab;
            $scope.param.relatedModuleName = item.selectTabContent;
            $scope.param.moduleNo = ""
        }
        $scope.chooseinfo = function() {
            // $routeParams.id通过id确定唯一回显id
            if (!$scope.param.relatedModule) {
                return
            }
            Util.savaParamForReview($scope.param, $routeParams.id);
            // 里面包含了title
            var relModuleObj = {
                name: "FinanceFBxdCtrl",
                selectTab: $scope.param.relatedModule,
                selectTabContent: $scope.param.relatedModuleName
            };
            Util.savaDataforsearch(relModuleObj);
            $location.path("finance/choosepeople")

        };

        $scope.sheetObj.relModuleObj = relModuleObj;
        $scope.showrelModule = function() {
            Util.getModuleRel(Util.OTH_MODULE, function(data) {
                $scope.sheetObj.relModuleObj.tabList = data.data
                $scope.showSheet("relModuleObj")
            })

        };
        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // 选择报销单明细
        $scope.showCertificate = function() {
            var param = {};
            param.type = "报销费用类型";
            Util.getKeyValue(param, function(data) {
                $scope.resiTypeList = data.data
                $rootScope.showMasker = true;
                $scope.showCerPicker = true;
            })
        };
        $scope.deletedItem = function(index) {
            $scope.param.details.splice(index, 1);
            changeTatalAcount();
        };
        $scope.addCertificate = function() {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.details.push($scope.picker);
            // 隐藏表单
            hideCertificate();
            changeTatalAcount();
        };
        var hideCertificate = function() {
            $scope.picker = {};
            $rootScope.showMasker = false;
            $scope.showCerPicker = false;
        };
        $scope.cacelCertificate = function() {
            hideCertificate();
        };
        // 验证是否输入完整
        var checkPicker = function(data) {
            var check = true;
            if (!data.resiType) {
                Util.Toast("请选择报销类型");
                check = false;
                return check;
            }
            if (!data.receiptsSum) {
                Util.Toast("请填写单据数量");
                check = false;
                return check;
            }
            if (!data.amount) {
                Util.Toast("请填写金额");
                check = false;
                return check;
            }
            return check;

        };
        // 监测列表变化改变总金额数值
        var changeTatalAcount = function() {
            var sum = 0;
            if (!$scope.param.details) {
                // 用来显示选择的报销明细
                $scope.param.details = $scope.param.details || []

                return $scope.totalAmount = "";
            }

            $scope.param.details.forEach(function(item, index) {
                sum = sum + parseInt(item.amount, 10);
            });
            $scope.totalAmount = sum;
        };
        // 提交申请
        $scope.submit = function() {
            if (!$scope.param.remark) {
                Util.Toast("请填写报销事由");
                return;
            }
            if ($scope.param.details.length < 1) {
                Util.Toast("请填写报销明细");
                return;
            }
            $scope.param.reimDetails = angular.toJson($scope.param.details);
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.reimbursement;
            var options = {};
            var myDest = {};
            options.urlParam = urlParam;
            options.param = angular.copy($scope.param, myDest);
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            // 不删除会失败
            delete options.param.details
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.reimbursement
            }
            new FormSubmit(options);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
                changeTatalAcount()
            });
        };
        // 初始化
        init();
        // 显示审批记录判断
        Util.showloglist($routeParams.id, function(data) {
            $scope.logData = data;
        });

    });
